package losrebellos.lospistoleros.core.registration 
{
	import losrebellos.lospistoleros.core.command.IPCommand;
	import losrebellos.lospistoleros.core.constants.PRegistrationConstants;

	/*
	 *
	 * @author los rebellos - ben@benoitvinay.com
	 *
	 */
	public class PRegistrationElement implements IPRegistrationElement
	{
		/*
		 * 
		 * CONSTRUCTOR
		 * 
		 */
		public function PRegistrationElement(value:*)
		{
			super();
			
			//save the element
			_element = value;
			
			//save the name
			_name = _element["NAME"];
			
			//save the type & constructor
			if(_element["TYPE"])
			{
				_type = _element["TYPE"];
			}
			else
			{
				//command type
				if(_element is IPCommand)
				{
					_type = PRegistrationConstants.TYPE_COMMAND;
					
					//save the class
					_constructor = value;
				}
				
				//not command type
				else
				{
					_type = getTypeFromElement(_element);
				
					//save the class
					_constructor = _element["constructor"];
				}
			}
		}

		
		/*
		 * 
		 * TYPE
		 * PDispatcherType
		 * 
		 */
		protected var _type:String = null;
		public function get type():String
		{
			return _type;
		}
		
		
		/*
		 * 
		 * TYPE
		 * constructor class
		 * 
		 */
		protected var _constructor:Object = null;
		public function get constructor():Object
		{
			return _constructor;
		}
		
		
		/*
		 * 
		 * NAME
		 * name inside the constants of the class
		 * 
		 */
		protected var _name:String = null;
		public function get name():String
		{
			return _name;
		}
		
		
		/*
		 * 
		 * ELEMENT
		 * element itself
		 * 
		 */
		protected var _element:* = null;
		public function get element():*
		{
			return _element;
		}
		
		
		/*
		 * 
		 * UTILS
		 * 
		 */
		public static function getTypeFromElement(value:*):String
		{
			for(var i:int = 0; i<PRegistrationConstants.TYPES_REGISTERED.length; i++)
			{
				if(value is PRegistrationConstants.TYPES_REGISTERED[i][0])
				{
					return PRegistrationConstants.TYPES_REGISTERED[i][1];
				}
			}
			
			return PRegistrationConstants.TYPE_UNKNOWN;
		}
	}
}